package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Productx;
import cn.jdy.plugins.service.MallService;
import cn.jdy.plugins.service.ProductxService;

/**
 * @title 产品信息控制类
 * @date 2014-03-12
 */
@Controller
@RequestMapping("/productx")
public class ProductxCtrl extends BaseCtrl {

	@Autowired
	private ProductxService productxService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/productx/list?s=";
	@Autowired
	private MallService mallService;
	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Productx entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/operate/productxList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				case '5': mv.addObject("msg", "审核通过！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		return mv;
	}

	/**
	 * 异步查询
	 * @param news
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Productx productx, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", productxService.query(productx, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", productxService.count(productx, fromDate, toDate));
			jsonMap.put("iTotalRecords", productxService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询产品异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/productxDetail.jsp");
		User user = getUser();
		
		mv.addObject("typelist", confService.queryByType(5, 0L));
		
		mv.addObject("qhlist", confService.queryByType(7, 0L));
		
		mv.addObject("hdbqlist", confService.queryByType(8, 0L));
		
		mv.addObject("mall", this.mallService.find("organid",user.getOrganid()));
		mv.addObject("data", id==null||id==0 ? id : this.productxService.find(id));
		return mv;
	}
	/**
	 * 保存（更新）产品
	 * @param news
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(Productx productx, Long pid) {
		ModelAndView mv = new ModelAndView();
		try {
			//如果二级产品类别为空，则使用一级类别
		  
     
			if(productx.getId()==null) {//新增
				User user = getUser();
				productx.setUserid(user.getId());
				productx.setUsername(user.getUsername());
				productx.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.productxService.save(productx);
				mv.setViewName(this.viewName.concat("1"));
			} else {// 更新
				User user = getUser();

				this.productxService.update(productx);
				mv.setViewName(this.viewName.concat("2"));
				// 清理内容页缓存
        NginxPurgeCacheUtil.purge(4, productx.getId());
			}
			// 清理栏目列表缓存
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "产品保存异常！", e);
		}
		return mv;
	}
/**
 * 审核通过
 * @param id
 * @return
 */
	@RequestMapping("/audit-{id}")
  public ModelAndView audit(@PathVariable Long id) {
    ModelAndView mv = new ModelAndView();
    try {
      Productx px=new Productx();
      px=this.productxService.find(id);
      
      px.setStatus("0");
        this.productxService.update(px);
        mv.setViewName(this.viewName.concat("5"));
        // 清理内容页缓存
        NginxPurgeCacheUtil.purge(4, px.getId());
      
      // 清理栏目列表缓存
    } catch(Exception e) {
      mv.setViewName(this.viewName.concat("4"));
      BaseLog.e(getClass(), "产品审核异常！", e);
    }
    return mv;
  }
	/**
	 * 删除产品
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.productxService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
		  // 清理内容页缓存
      NginxPurgeCacheUtil.purge(4, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除产品异常！", e);
		}
		return mv;
	}

}
